
	package br.uniminas.persistencia;

	import java.util.List;
	import org.hibernate.Query;
	import org.hibernate.Session;
	import org.hibernate.Transaction;
import br.uniminas.entidades.Fornecedor;


	public class FornecedorDao {

		private List<Fornecedor> listaFornecedores;
		private Fornecedor fornecedor;
		private Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();

		public List todosFornec() {
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				listaFornecedores = session.createQuery("from Fornecedor").list();
				return listaFornecedores;
			} finally {
				session.close();
			}
		}

		public List getAllFornecedor() {
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				listaFornecedores = session.createQuery("from Fornecedor").list();
				return listaFornecedores;
			} finally {
				session.close();
			}
		}

		
		
		public Fornecedor getFornecedor(Integer id) {
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session.createQuery("from Fornecedor as e where e.id =:id");
				q.setInteger("id", id);
				return (Fornecedor) q.uniqueResult();
			} finally {
				session.close();
			}
		}

		public List<Fornecedor> buscaFornecedoresPorCnpj(String cnpj) {
			System.out.println(cnpj);
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session.createQuery("from Fornecedor as f where f.cnpj like :cnpj");
				q.setString("cnpj", "%"+cnpj+"%");
				List l = q.list();
				return l;
			} finally {
				session.close();
			}
		}
		
		
		/*
		
		public Fornecedor buscaFornecedorPorCnpj(String cnpj) {
			System.out.println(cnpj);
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session.createQuery("from Fornecedor as f where f.cnpj = :cnpj");
				q.setString("cnpj", "%" + cnpj + "%");
				return (Fornecedor) q.uniqueResult();
			} finally {
				session.close();
			}
		}
	*/
		public List<Fornecedor> buscaFornecedorPorCidade(String cidade) {
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session
						.createQuery("from Fornecedor as f where f.cidade = :cidade");
				q.setString("cidade", "%" + cidade + "%");
				List l = q.list();
				return l;
			} finally {
				session.close();
			}
		}

		public List<Fornecedor> buscaFornecedorPorNome(String razaoSoc) {
			System.out.println(razaoSoc);
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session
						.createQuery("from Fornecedor as f where f.razaoSoc like :razaoSoc");
				q.setString("razaoSoc", "%" + razaoSoc + "%");
				List l = q.list();
				return l;
			} finally {
				session.close();
			}
		}

		public void criaRegistroFornecedor(Fornecedor fornec) {
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction tx = null;
			try {
				tx = session.beginTransaction();
				session.save(fornec);
				tx.commit();
			} catch (RuntimeException e) {
				if (tx != null)
					tx.rollback();
				throw e;
			} finally {
				session.close();
			}
		}

		public Fornecedor pegaFornecedor(String cnpj) {
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session.createQuery("from Fornecedor as e where e.cnpj =:cnpj");
				q.setString("cnpj", cnpj);
				return (Fornecedor) q.uniqueResult();
			} finally {
				session.close();
			}
		}
		
		
		
		public void deleteFornecedor(Integer id) {
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction tx = null;
			try {
				tx = session.beginTransaction();
				fornecedor = (Fornecedor) session.get(Fornecedor.class, id);
				session.delete(fornecedor);
				tx.commit();
			} catch (RuntimeException e) {
				if (tx != null)
					tx.rollback();
				throw e;
			} finally {
				session.close();
			}
		}

		public void updateFornecedor(Fornecedor fornec) {
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction tx = null;
			try {
				tx = session.beginTransaction();
				session.update(fornec);
				tx.commit();
			} catch (RuntimeException e) {
				if (tx != null)
					tx.rollback();
				throw e;
			} finally {
				session.close();
			}
		}
	}

